<?php
require("const.php");
require("SSH2.php");
require("function.php");

use ssh\SSH2;

$file = $_GET["file"] ?? "";
$ip = $_GET["ip"] ?? "";
$Action = "insert";
$load = "";
$reloadFile = "";
$reloadIP = "";
$content = "";
$originContent = "";
try {
    if (IS_REMOTE) {
        if (empty(REMOTE_MAP)) {
            echo "无集群配置";
            exit;
        } else {
            if (isset($_POST['Action'])) {
                if ($_POST['Action'] === 'update') {
                    $ssh = new SSH2(REMOTE_MAP[$ip]['ssh']);
                    $localPath = "./conf/{$ip}/vhost/" . $file;
                    $fn = fopen($localPath, "w");
                    fwrite($fn, $_POST['content']);
                    $fn = fclose($fn);
                } else {
                    $file = $_POST["file"];
                    $ip = $_POST["ip"];
                    $ssh = new SSH2(REMOTE_MAP[$ip]['ssh']);
                    $localPath = "./conf/{$ip}/vhost/" . $file;
                    $fn = fopen($localPath, "w");
                    fwrite($fn, $_POST['content']);
                    $fn = fclose($fn);
                    $load = "reload";
                    $reloadFile = $file;
                    $reloadIP = $ip;
                }
                $ssh->sendFile($localPath, rtrim(REMOTE_MAP[$ip]['vhosts_path'], "/") . "/" . $file);
                $ssh->cmd(REMOTE_MAP[$ip]['reload_nginx_cmd']);
            }
        }
    } else {
        echo "非集群配置";
        exit;
    }
} catch (\Exception $e) {
    var_dump($e->getMessage());
}

if (isset($file) && trim($file) !== "") {
    $Action = "update";
    $filePath = "./conf/{$ip}/vhost/" . $file;
    [$originContent, $content] = getContent($filePath);
}
?>
<link rel="stylesheet" type="text/css" href="css/index.css">
<body>
<div id="conf-box">
    <?php echo $content; ?>
</div>
<form class="detail" method="post">
    <input type="hidden" id="Action" name="Action" value="<?php echo $Action; ?>">
    <input type="hidden" id="file" name="file" value="">
    <input type="hidden" id="ip" name="ip" value="">
    <textarea id="content" name="content" class="content"><?php echo $originContent; ?></textarea>
</form>
</body>
<script src="js/index.js" type="text/javascript"></script>
<script>
    if ("<?php echo $load; ?>" === "reload") {
        window.parent.location.href = "remote.php?reloadFile=<?php echo $reloadFile; ?>&reloadIP=<?php echo $reloadIP; ?>";
    }
    document.onkeydown = function (e) {
        let currentKey = 0;
        e = e || event || window.event;
        currentKey = e.keyCode || e.which || e.charCode;
        if (currentKey === 83 && (e.ctrlKey || e.metaKey)) {
            e.preventDefault();
            let Action = document.querySelector("#Action").value;
            if (Action === "update") {
                document.forms[0].submit();
            } else {
                let tmp = window.prompt("请输入保存的文件名(ip:*.conf)：");
                if (tmp === null || tmp.trim() === "") {
                    alert("文件名不能为空");
                    return false;
                }
                let arr = tmp.split(":");
                let id = arr[1].replace(/\./g, "-");
                if (window.parent.document.querySelector('#' + id) !== null) {
                    alert("配置文件已存在，请重新输入文件名");
                    return false;
                }
                document.querySelector('#file').value = arr[1];
                document.querySelector('#ip').value = arr[0];
                document.forms[0].submit();
            }
        } else {
            if (currentKey === 27) {
                document.querySelector('#conf-box').style.display = "block";
                document.querySelector("form").style.display = "none";
            }
        }
    }
</script>